package webService;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import serverDB.Box;
import serverDB.Client;
import serverDB.ConnectToDataBase;
import serverManger.Server;

/**
 * create new box
 * @author ran, barak
 *
 */
public class NewBoxServlet extends HttpServlet{
	/**
	 * Default serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * get the post request 
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// get the connection to the server and the session
		ConnectToDataBase connectToDataBase = ConnectToDataBase.getInst();
		HttpSession session = req.getSession();
		// get the client
		Client client = connectToDataBase.regetClient((Client)session.getAttribute("client"));
		String boxName = req.getParameter("boxName");
		Box box = connectToDataBase.getBox(client,boxName);
		// if the box is alredy exist
		if(box!=null){
			session.setAttribute("error","This box is allready exist!");
			resp.sendRedirect("newBox.jsp");
		}
		// creat the new box
		Server server = Server.getServerInst();
		server.newBox(boxName,client,null);
		resp.sendRedirect("logedIn2.jsp");
	}
}
